package Main;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;

import tasks.Boiler;
import tasks.BoilerControlSystem;

import descentMethods.SimpleIterationDescent;

import functionTypes.ConstantSampling;
import functionTypes.Interpolation;
import functionTypes.OldSinglePropertyFunction;
import genetic.Cellular;
import genetic.ClassicGenetic;
import genetic.HillClimbing;
import genetic.Island;

public class Main {
	public static BoilerControlSystem getNext(BufferedReader br)
			throws Exception {
		ConstantSampling[] f = new ConstantSampling[5];
		for (int j = 0; j != 5; ++j) {
			f[j] = new ConstantSampling();
		}

		StringTokenizer st = new StringTokenizer(br.readLine());

		int i = 0;
		for (; i != 200; ++i) {
			if (st.nextToken().charAt(0) == '/') {
				if (i > 100) {
					for (; i != 200; ++i) {
						for (int k = 0; k != 5; ++k) {
							f[k].subfunctions[i] = f[k].subfunctions[i - 1]
									.clone();
						}
					}
				} else {
					st = new StringTokenizer(br.readLine());
				}
			} else {
				for (int k = 0; k != 5; ++k) {
					f[k].subfunctions[i].getCoeff()[0] = Double.parseDouble(st
							.nextToken());
				}
				st = new StringTokenizer(br.readLine());
			}
		}

		BoilerControlSystem cs = new BoilerControlSystem(f);
		return cs;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader("input.txt"));
		StringTokenizer st = new StringTokenizer(br.readLine());
		PrintWriter pw = new PrintWriter("output.txt");

		int fieldSize = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine());
		double mutationProbability = Double.parseDouble(st.nextToken());

		/*for (int k = 0; k != -1; ++k) {
			System.out.println("//////////////////////////\n" + k
					+ "//////////////////////////");
			HillClimbing algo = new HillClimbing("boiler", "interpolation", 40,300, 30);

			// Island algo = new Island("boiler", "interpolation",5,5,
			// 500,0.03);

			pw.println("//////////////////////////\n" + k
					+ "//////////////////////////");
			pw.println("top");

			
				pw.println(algo.bestIndividualScore);
			// for (int i = 0; i != algo.eachStepBestScore.length; ++i) {
			// pw.println(algo.eachStepBestScore[i]);
			// }

			if (algo.bestIndividualScore<4.4E8){
				PrintWriter best = new PrintWriter("best.txt");
				for (int i=0;i!=5;++i){
					for (double j=40;j<99;j+=0.25){
						best.print(algo.bestIndividual.getFunctions()[i].compute(new double[]{j})+" ");
					}
					best.println();
				}
				best.close();
				
				PrintWriter best2 = new PrintWriter("best2.txt");
				for (int i=0;i!=5;++i){
					for (int j=algo.bestIndividual.getFunctions()[i].getSubFunctions().length-1;j>=0;j-=1){
						best2.print(algo.bestIndividual.getFunctions()[i].getSubFunctions()[j].getCoeff()[0]+" ");
					}
					best2.println();
				}
				best2.close();
				
				break;
			}
			
			pw.println();
			pw.println();
			pw.println();
			pw.println();
			pw.println();
			pw.flush();
		}
*/
		
		BoilerControlSystem ind = new BoilerControlSystem(); 
		
		PrintWriter best = new PrintWriter("best.txt");
		BufferedReader best2 = new BufferedReader(new FileReader("best2.txt"));
		StringTokenizer memb;
		for (int i=0;i!=5;++i){
			memb = new StringTokenizer(best2.readLine());
			ind.getFunctions()[i] = new Interpolation();
			OldSinglePropertyFunction[] func = new OldSinglePropertyFunction[10];
			for (int j=0;j!=10;++j){
				func[j] = new OldSinglePropertyFunction();
				func[j].setConstantType();
				func[j].coeff = new double[]{Double.parseDouble(memb.nextToken())};
			}
			ind.getFunctions()[i].createAbstractFunction(func);
			((Interpolation)(ind.getFunctions()[i])).calcCoeff();
		}
		for (int i=0;i!=5;++i){
			for (double j=0;j<99;j+=0.25){
				best.print(ind.getFunctions()[i].compute(new double[]{j})+" ");
			}
			best.println();
		}
		best.close();
		best2.close();
		
		// / cellular
		/*
		 * for (int k = 0; k != 10; ++k) {
		 * System.out.println("//////////////////////////\n" + k +
		 * "//////////////////////////"); Cellular algo = new Cellular("boiler",
		 * "sampling", fieldSize, 10000, mutationProbability);
		 * 
		 * pw.println("//////////////////////////\n" + k +
		 * "//////////////////////////"); pw.println("top"); for (int i = 0; i
		 * != algo.midScores.length; ++i) { pw.println(algo.topScores[i]); }
		 * pw.println(); pw.println(); pw.println(); pw.println(); pw.println();
		 * pw.flush(); }
		 */
		// / descent
		/*
		 * for (int k = 0; k < 1; ++k) { System.out.println("///////////++" + k
		 * + "++///////////////"); pw.println("///////////++" + k +
		 * "++///////////////");
		 * 
		 * SimpleIterationDescent algo = new SimpleIterationDescent("boiler",
		 * "sampling", null); for (int i = 0; i !=
		 * algo.eachStepBestScore.length; ++i) {
		 * pw.println(algo.eachStepBestScore[i]); } Boiler boiler = new
		 * Boiler(); BoilerControlSystem[] cs = new BoilerControlSystem[5];
		 * double[] res = new double[5]; br = new BufferedReader(new
		 * FileReader("sample functions best.txt")); for (int j = 0; j != 5;
		 * ++j) { cs[j] = getNext(br); res[j] = boiler.getEffectivnes(cs[j]);
		 * algo.individual = cs[j]; algo.individualScore = res[j];
		 * algo.constantSamplingBoilerSimpleIteration();
		 * pw.println("///////////++" + j + "++///////////////"); for (int i =
		 * 0; i != algo.eachStepBestScore.length; ++i) {
		 * pw.println(algo.eachStepBestScore[i]); } }
		 * 
		 * pw.flush(); }
		 */

		// / hill climbing
		/*
		 * for (int k = 0; k < 5; ++k) { System.out.println("///////////++" + k
		 * + "++///////////////"); pw.println("///////////++" + k +
		 * "++///////////////");
		 * 
		 * HillClimbing algo = new HillClimbing("boiler", "sampling", 55, 300,
		 * 0.35); for (int i = 0; i != algo.bestIndividualsScores.length; ++i) {
		 * pw.println(algo.bestIndividualsScores[i]); } pw.flush(); }
		 */
		/*
		 * HillClimbing algo = new HillClimbing("boiler", "sampling", 5, 10,
		 * 0.35); Boiler boiler = new Boiler(); BoilerControlSystem[] cs = new
		 * BoilerControlSystem[5]; double[] res = new double[5]; for (int
		 * i=1;i!=2;++i){ br = new BufferedReader(new
		 * FileReader("sample functions best.txt")); for (int j=0;j!=5;++j){
		 * cs[j] = algo.getNext(br); res[j] = boiler.getEffectivnes(cs[j]); }
		 * algo.individuals = cs; algo.scores = res; algo.bestIndividual =
		 * algo.individuals[0]; algo.bestIndividualScore = algo.scores[0];
		 * algo.bestIndividualsScores = new double[10001]; algo.individualsCount
		 * = 5; algo.maxEvolutionCount = 10000; algo.mutationProbability = 0.05;
		 * System.out.println("////////////////////////////////////"); //
		 * 4.895029498077964E8 4.971144328174876E8 5.160760304193386E8
		 * 5.434340501079402E8 5.448696754514803E8; algo.boilerHillClimbing(); }
		 */
		pw.close();
	}
}
